
package com.gp.activity.detail;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gp.R;
import com.gp.activity.base.BaseActivity;
import com.gp.activity.main.MyWayActivity;
import com.gp.utils.DatabaseManager;

public class MywayDetailActivity extends BaseActivity {
    // 查看状态
    public static final int CHECK_STATE = 0;
    // 编辑状态
    public static final int EDIT_STATE = 1;
    // 修改状态
    public static final int ALERT_STATE = 2;
    

    private int state = -1;

    private Button mbtnSave;
    private EditText metTitle;
    private EditText metConten;
    private TextView mtvTime;
    private TextView mtvtitle;
    private DatabaseManager mDatabaseManager;

    private String id;
    private String titleText;
    private String contentText;
    private String timeText;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myway_detail);

        mDatabaseManager = new DatabaseManager(this);
        initView();
        getData();
        initListener();

    }

    private void initView() {

        mbtnSave = (Button) findViewById(R.id.btn_myway_detail_save);
        metTitle = (EditText) findViewById(R.id.et_myway_detail_title);
        metConten = (EditText) findViewById(R.id.et_myway_detail_content);
        mtvTime = (TextView) findViewById(R.id.tv_myway_detail_time);
        mtvtitle = (TextView) findViewById(R.id.tv_myway_title_text);
        
       
    }

    private void getData() {
        
        // 得到穿过来的数据
        Bundle qBundle = getIntent().getExtras();
        state = qBundle.getInt("state");

        // 得到修改前内容
        if (state == CHECK_STATE||state == ALERT_STATE) {
            
            id = qBundle.getString("id");
            titleText = qBundle.getString("title");
            contentText = qBundle.getString("content");
            timeText = qBundle.getString("time");
            
            metTitle.setText(titleText);
            metConten.setText(contentText);
            
            String datetime = DateFormat.format("yyyy/MM/dd kk:mm",
                    Long.parseLong(timeText)).toString();
            mtvTime.setText(datetime);
     
           
            if (state == CHECK_STATE) {
                 //查看状态下不可编辑
                metTitle.setFocusable(false);
                metConten.setFocusable(false);
                mtvtitle.setText("查看足迹");
                //按钮不可见
                mbtnSave.setVisibility(View.INVISIBLE);
            }
            if (state == ALERT_STATE) {
                
                mtvtitle.setText("修改足迹");
            }
            
            
            
        }

    }

    private void initListener() {

        mbtnSave.setOnClickListener(new SaveButtonListener());
        
       
       
        
    }

    public class SaveButtonListener implements OnClickListener {

        public void onClick(View v) {
            
            titleText = metTitle.getText().toString();
            contentText = metConten.getText().toString();

            try {
                mDatabaseManager.open();
                // 编辑
                if (state == EDIT_STATE) { 
                    mDatabaseManager.insert(titleText, contentText);
                }
                // 修改
                if (state == ALERT_STATE) {
                    // id 强制转换成int型
                    mDatabaseManager.update(Integer.parseInt(id), titleText,
                            contentText); 
                }
                mDatabaseManager.close();

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            
            
            /*Intent intent = new Intent();
            intent.setClass(MywayDetailActivity.this, MyWayActivity.class);
            MywayDetailActivity.this.startActivity(intent);*/
            MywayDetailActivity.this.finish();
        }

    }

}
